---
import type { ImportType, InternalFramework } from '@ag-grid-types';
import FrameworkTemplate from '@features/example-runner/components/FrameworkTemplate.astro';
import { getExampleUrl } from '../utils/urlPaths';
import { getGeneratedContents } from '@features/example-generator';

interface Props {
    internalFramework: InternalFramework;
    pageName: string;
    exampleName: string;
    importType: ImportType;
    /**
     * Whether to use relative paths for script files references
     */
    relativePath?: boolean;
    /**
     * Whether to add a script to post an `init` message to the parent
     */
    addInitMessageScript?: boolean;
    /**
     * Whether to not include system js code
     */
    ignoreSystemJs?: boolean;
    /**
     * Extra styles to add to HTML
     */
    extraStyles?: string;
}

const { internalFramework, pageName, exampleName, importType, relativePath, addInitMessageScript, ignoreSystemJs, extraStyles } =
    Astro.props as Props;

const contentParams = {
    framework: internalFramework,
        pageName,
        importType,
        exampleName,
}
let generatedContents;
try {
    generatedContents = await getGeneratedContents({
        type: 'docs',
        ...contentParams
    });
} catch(error) {
    console.error('Error generating content for:', contentParams);
    console.error(error);
}

const exampleUrl = getExampleUrl({
    internalFramework,
    pageName,
    exampleName,
    importType
});
---
{!generatedContents && <p>Error generating content for: {JSON.stringify(contentParams)}</p>}
{generatedContents && <FrameworkTemplate
    internalFramework={internalFramework}
    pageName={pageName}
    exampleName={exampleName}
    exampleUrl={exampleUrl}
    generatedContents={generatedContents!}
    relativePath={relativePath}
    addInitMessageScript={addInitMessageScript}
    ignoreSystemJs={ignoreSystemJs}
    extraStyles={extraStyles}
/>}
